/**
 * BootStrap工具类
 * @namespace gui.widgets
 */
gui.widgets.BootStrap = function() {
	return {
		//成功提示 content内容 title标题 postion位置样式（gritter-center)
		alertOk:function(content,title,postion){
			var className = postion ? " " + postion:" gritter-center";
			$.gritter.add({
				title: title ? title:'成功提示',
				text: content,
				time: 1000, 
				class_name: "gritter-success" + className
			});
		},
		alertWarn:function(content,title,postion){
			var className = postion ? " " + postion:" gritter-center";
			$.gritter.add({
				title: title?title:'警告提示',
				text: content,
				time: 1000, 
				class_name: 'gritter-warn' + className
			});
		},
		alertError:function(content,title,postion){
			var className = postion ? " " + postion:" gritter-center";
			$.gritter.add({
				title: title?title:'错误提示',
				text: content,
				time: 3000, 
				class_name: 'gritter-error' + className
			});
		},
		confirm:function(msg,obv){
			bootbox.confirm(msg, function(result) {
				if(result) {
					obv.call(window);
				}
			});
		},
		validateForm:function(dom,rules,obv){
			//重置验证
    		$(".has-error").removeClass("has-error");
    		$(".help-block").remove();
    		
    		$(dom).validate({
				errorElement: 'div',
				errorClass: 'help-block',
				focusInvalid: false,
				rules: rules,
				invalidHandler: function (event, validator) { //display error alert on form submit   
					$('.alert-danger', $('.login-form')).show();
				},
				highlight: function (e) {
					$(e).closest('.form-group').removeClass('has-info').addClass('has-error');
				},
				success: function (e) {
					$(e).closest('.form-group').removeClass('has-error').addClass('has-info');
					$(e).remove();
				},
				errorPlacement: function (error, element) {
					if(element.is(':checkbox') || element.is(':radio')) {
						var controls = element.closest('div[class*="col-"]');
						if(controls.find(':checkbox,:radio').length > 1) controls.append(error);
						else error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
					}
					else if(element.is('.select2')) {
						error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
					}
					else if(element.is('.chosen-select')) {
						error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
					}
					else error.insertAfter(element.parent());
				},
				submitHandler: function (f) {
					obv.call(window,f);
				},
				invalidHandler: function (form) {
				}
			});
		},
		//jqGrid分页 options{gridSelector,colNames,colModel,height,pagerSelector,caption}
		loadGrid:function(options){
			jQuery(options.gridSelector).jqGrid({
				url:options.url,
				postData:options.postData,
				datatype: "json",
				mtype:'post',
				jsonReader:options.jsonReader,
				prmNames:options.prmNames,
				colNames:options.colNames,//['名称','修改时间','操作']
				colModel:options.colModel, 
				height:options.height?options.height:"auto",
				viewrecords : true,
				rowNum:options.rowNum?options.rowNum:10,
				shrinkToFit:options.shrinkToFit?options.shrinkToFit:true,
				autoScroll: options.autoScroll?options.autoScroll:true,
				rowList:[10,20,30],
				pager : options.pagerSelector,
				altRows: false,
				multiselect: true,
		        multiboxonly: true,
				loadComplete : function() {
					var table = this;
					setTimeout(function(){
						updatePagerIcons(table);
						enableTooltips(table);
					}, 0);
				},
				caption: options.caption,
				autowidth: true
			});
			
			function updatePagerIcons(table) {
				var replacement = 
				{
					'ui-icon-seek-first' : 'icon-double-angle-left bigger-140',
					'ui-icon-seek-prev' : 'icon-angle-left bigger-140',
					'ui-icon-seek-next' : 'icon-angle-right bigger-140',
					'ui-icon-seek-end' : 'icon-double-angle-right bigger-140'
				};
				$('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function(){
					var icon = $(this);
					var $class = $.trim(icon.attr('class').replace('ui-icon', ''));
					
					if($class in replacement) icon.attr('class', 'ui-icon '+replacement[$class]);
				})
			}
		
			function enableTooltips(table) {
				$('.navtable .ui-pg-button').tooltip({container:'body'});
				$(table).find('.ui-pg-div').tooltip({container:'body'});
			}
		},
		colButton:function(icon,title,funName){
			return "<div title=\"\" style=\"float:left;margin-left:5px;\" class=\"ui-pg-div ui-inline-del\" id=\"jDeleteButton_5\" onclick=\"" + funName + "\" onmouseover=\"jQuery(this).addClass('ui-state-hover');\" onmouseout=\"jQuery(this).removeClass('ui-state-hover');\" data-original-title=\"" + title + "\"><span class=\"ui-icon " + icon + "\"></span></div>";
		}
	}
}();